package hospital;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.test.R;

public class quiz_answer_ok extends Activity{
    /** Called when the activity is first created. */
	//AlertDialog.Builder nologin;
	//String[] resultString =null;
	static int check=0;
	TextView answer_good_text;
   // ImageView levelup_character_img;
    //ImageView coupon_img;
    TextView quiz_pointget_text;
    TextView mycoupon_sevicelife_text;
    //int level_count = 0;
    private SharedPreferences sharedPref;
	private SharedPreferences.Editor sharedEditor;
    
    //Bitmap avatar_img;
	//String avatar_link = "";
    //MediaPlayer mp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quiz_answer_ok);
        
        answer_good_text = (TextView) findViewById(R.id.answer_good_text);
        //levelup_character_img = (ImageView) findViewById(R.id.levelup_character_img);
        //coupon_img = (ImageView) findViewById(R.id.coupon_img);
        mycoupon_sevicelife_text = (TextView) findViewById(R.id.mycoupon_sevicelife_text);
        //quiz_pointget_text = (TextView) findViewById(R.id.quiz_pointget_text);
        ImageView answer_title = (ImageView)findViewById(R.id.answer_title);
        //coupon_img.setVisibility(0x00000000);
        sharedPref = getSharedPreferences("pref",Activity.MODE_PRIVATE);
		//sharedEditor = sharedPref.edit();
		
		int a = sharedPref.getInt("Month", 99);
		int b = sharedPref.getInt("Day", 99);
		

		String str1 = Integer.toString(a);
		String str2 = Integer.toString(b);
		String tmp = "쿠폰사용기간 : " + str1 + "월" + str2 + "일 까지";
		mycoupon_sevicelife_text.setText(tmp);
		check = 1;
        //if(items.event_list.get(items.quiz_no).getQuiz_type() == 4){
        	answer_title.setImageResource(R.drawable.oxquiz_title);
        	//mycoupon_sevicelife_text.setText(" 1층 로비에서 교환하세요~ ");
        	answer_good_text.setText("문제5의 설명");
        	/*}else{
        	
        	final ProgressDialog nowloding = ProgressDialog.show(quiz_answer_ok.this,"","서버에 접속 중 입니다....",true);
            answer_title.setImageResource(R.drawable.oxquiz_title);
    		
    		new Thread(new Runnable() {
    			@Override
    			public void run() {
    				URL url = null;
    		        try {
    		        	
    		        	url = new URL(tour_lookback_insert+"?tour_position="+items.event_position+"&app_type="+items.apptype+"&c_code="+items.c_code);
    				
    			        HttpURLConnection http = (HttpURLConnection)url.openConnection();  
    			        
    					BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));  
    					StringBuffer buffer = new StringBuffer();
    					 
    					int c;
    					
    					while((c=in.read()) != -1){
    						buffer.append((char)c);
    					}
    					
    					resultString= buffer.toString().trim().split(",");
    					
    		        	
    					nowloding.cancel();
    					
    					level_count = 11-Integer.parseInt(resultString[3].trim());
    					
    					runOnUiThread(new Runnable(){
    			            @Override
    			             public void run() {
    			            	answer_good_text.setText(resultString[2].trim());
    			            	if(resultString[0].trim().equals("1")){
    			            		mp = MediaPlayer.create(quiz_answer_ok.this, R.raw.coin);
    					            mp.start();
    					            
    			            		for(int i=0; i<items.level_up_rank.length;i++){
    			            			if(Integer.parseInt(resultString[3].trim()) == items.level_up_rank[i]){
    				            			levelup_img.setVisibility(0x00000000);
    				            			if(Integer.parseInt(resultString[3].trim()) == 3){
    											if(items.c_sex == 1){
    						            			avatar_link = items.sunbie_avatat_link;
    						            		}else{
    						            			avatar_link = items.dortor_avatat_link;
    						            		}
    										}else if(Integer.parseInt(resultString[3].trim()) == 5){
    											if(items.c_sex == 1){
    						            			avatar_link = items.byungjol_avatat_link;
    						            		}else{
    						            			avatar_link = items.goongnyu_avatat_link;
    						            		}
    										}else if(Integer.parseInt(resultString[3].trim()) == 7){
    											if(items.c_sex == 1){
    						            			avatar_link = items.janggun_avatat_link;
    						            		}else{
    						            			avatar_link = items.wjanggun_avatat_link;
    						            		}
    										}else if(Integer.parseInt(resultString[3].trim()) == 11){
    											if(items.c_sex == 1){
    						            			avatar_link = items.jungjo_avatat_link;
    						            		}else{
    						            			avatar_link = items.joongjun_avatat_link;
    						            		}
    										}
    						            	
    						            	URLConnection conn = null;
    										InputStream is= null;
    										URL imageURL;
    										
    		        						resultString = "오답1";
    										try {
    							            	
    				        					imageURL = new URL(avatar_link);
    				        					conn = imageURL.openConnection();
    				        					conn.connect();
    				        					is = conn.getInputStream();
    				        		 
    				        					BufferedInputStream bis = new BufferedInputStream(is);
    				        					avatar_img = BitmapFactory.decodeStream(bis);
    				        					bis.close();
    				        					is.close();
    				        					
    				        					levelup_character_img.setImageBitmap(avatar_img);	//링크주소 이미지 뷰에 띄우기
    				        					
    										} catch (MalformedURLException e) {
    				        					// TODO Auto-generated catch block
    				        					e.printStackTrace();
    				        				} catch (IOException e) {
    				        					// TODO Auto-generated catch block
    				        					e.printStackTrace();
    				        				}
    				            		}
    			            		}
    			            		if(level_count == 0){
    			            			quiz_pointget_text.setText(items.event_point_final + " 포인트 획득! 수고 하셨습니다");
    			            		}else{
    			            			quiz_pointget_text.setText(items.event_point_final + " 포인트 획득! 남은 미션 "+level_count+"개");
    			            		}
    							}else{
    								quiz_pointget_text.setText("이미 플레이한 미션입니다");
    							}
    			            	items.event_point_final=0;
    			             }
    			        });
    					
    		        } catch (MalformedURLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					runOnUiThread(new Runnable(){
    			            @Override
    			             public void run() {
    			            	nologin = new AlertDialog.Builder(quiz_answer_ok.this);
    			    			nologin.setTitle(items.appname);
    			    			nologin.setMessage("서버에 접속이 되지 않습니다\n네트워크를 확인 후 다시 시도해 주세요");
    			    			nologin.setIcon(R.drawable.icon);
    			    			nologin.setPositiveButton("확 인", new DialogInterface.OnClickListener(){
    			    				@Override
    			    				public void onClick(DialogInterface dialog, int which) {
    			    					// TODO Auto-generated method stub
    			    					moveTaskToBack(true);
    			    					android.os.Process.killProcess(android.os.Process.myPid());
    			    				}
    			    			});
    			    			nologin.show();
    			             }
    			        });
    					e.printStackTrace();
    				}
    			}
    	    }).start();
        }*/
        //items.quiz_no=0;
        //items.event_list.removeAll(items.event_list);
        //items.event_called = false;
    }
    public boolean onKeyDown( int KeyCode, KeyEvent event ){
		if( event.getAction() == KeyEvent.ACTION_DOWN ){
			if( KeyCode == KeyEvent.KEYCODE_BACK ){
				items.quiz_no=0;
				check = 1;
				Intent intent = new Intent(quiz_answer_ok.this, hospital_main.class);
				startActivityForResult(intent, 0);
				finish();
				
				return false;
			}
		}
		return super.onKeyDown(KeyCode, event);
	}
}